package com.sokal.easycare.modules.order.web.capital;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.order.entity.capital.PersonCapital;
import com.sokal.easycare.modules.order.service.capital.PersonCapitalService;

/**
 * 用户质保金Controller
 * @author lihaipei
 * @version 2017-05-26
 */
@Controller
@RequestMapping(value = "${adminPath}/order/capital/personCapital")
public class PersonCapitalController extends BaseController {

	@Autowired
	private PersonCapitalService personCapitalService;
	
	@ModelAttribute
	public PersonCapital get(@RequestParam(required=false) String id) {
		PersonCapital entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = personCapitalService.get(id);
		}
		if (entity == null){
			entity = new PersonCapital();
		}
		return entity;
	}
	
	@RequiresPermissions("order:capital:personCapital:view")
	@RequestMapping(value = {"list", ""})
	public String list(PersonCapital personCapital, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PersonCapital> page = personCapitalService.findPage(new Page<PersonCapital>(request, response), personCapital); 
		model.addAttribute("page", page);
		return "modules/order/capital/personCapitalList";
	}

	@RequiresPermissions("order:capital:personCapital:view")
	@RequestMapping(value = "form")
	public String form(PersonCapital personCapital, Model model) {
		model.addAttribute("personCapital", personCapital);
		return "modules/order/capital/personCapitalForm";
	}

	@RequiresPermissions("order:capital:personCapital:edit")
	@RequestMapping(value = "save")
	public String save(PersonCapital personCapital, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, personCapital)){
			return form(personCapital, model);
		}
		personCapitalService.save(personCapital);
		addMessage(redirectAttributes, "保存质保金成功");
		return "redirect:"+Global.getAdminPath()+"/order/capital/personCapital/?repage";
	}
	
	@RequiresPermissions("order:capital:personCapital:edit")
	@RequestMapping(value = "delete")
	public String delete(PersonCapital personCapital, RedirectAttributes redirectAttributes) {
		personCapitalService.delete(personCapital);
		addMessage(redirectAttributes, "删除质保金成功");
		return "redirect:"+Global.getAdminPath()+"/order/capital/personCapital/?repage";
	}

}